package com.gome.han.cloud.hystrix.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.gome.han.cloud.hystrix.service.UserService;
import com.gome.han.springcloud.api.bean.User;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;

/**
* @author hanpeng
* @version 创建时间：2020年8月8日 下午12:52:25
* @ClassName 类名称
* @Description 类描述
*/
@RestController
public class UserDDDController {
	@Autowired
	private UserService userService;
	//获取一些配置信息
	@Autowired
	private DiscoveryClient discoveryClient;
	
	
	@RequestMapping("/getUserDDD/{age}")
	@HystrixCommand(fallbackMethod="hystrix111")
	public User getUserDDD(@PathVariable(name="age") Integer age) {
		System.out.println("provide8083");
		User user = userService.getUser(age);
		return user;
	}
	public User hystrix111(@PathVariable(name="age") Integer age) {
		User user = new User();
		user.setAge(age);
		user.setName("不存在的hystrix");
		return user;
	}
	
	@RequestMapping("/listService/{id}")
	public Object discovery(@PathVariable(name="id") Integer id) {
		List<String> services = discoveryClient.getServices();
		System.out.println("discovery===> "+services);
		List<ServiceInstance> instances = discoveryClient.getInstances("SPRING-CLOUD-PROVIDER1");
		for (ServiceInstance instance:instances) {
			System.out.println(
					instance.getHost()+"\t"+
				    instance.getPort()+"\t"+
		     		instance.getInstanceId()+"\t"+
		     		instance.getScheme()+"\t"+
		     		instance.getServiceId()+"\t"+
		     		instance.getUri()+"\t"
					);
		}
		return this.discoveryClient;
	}
}
